package com.ytjj.bigdata.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.bigdata.dao.AdsPvCountMapper;
import com.ytjj.bigdata.entity.AdsPvCount;
import com.ytjj.bigdata.model.dto.AdsPvCountDto;
import com.ytjj.bigdata.model.response.AdsPvCountResponse;
import com.ytjj.common.api.CommonPageExtend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class AdsPvCountService {
    @Resource
    private AdsPvCountMapper adsPvCountMapper;

    public CommonPageExtend selectAdsPv(AdsPvCountDto adsPvCountDto) {
//        AdsPvCountResponse response = new AdsPvCountResponse();
        PageHelper.startPage(adsPvCountDto.getPageNum(),adsPvCountDto.getPageSize());
        List<AdsPvCount> list = adsPvCountMapper.selectAdsPv(adsPvCountDto);

        if (CollectionUtils.isEmpty(list)) {
            return new CommonPageExtend();
        }

        List<AdsPvCountResponse> pvCount = adsPvCountMapper.findPvCount(adsPvCountDto);
        CommonPageExtend pageInfo = CommonPageExtend.restPage(list);
        AdsPvCountResponse response = pvCount.get(0);
        pageInfo.setPvCount(response.getPvCount());
        pageInfo.setUvCount(response.getUvCount());

        return pageInfo;


//        AdsPvVo pvVo = new AdsPvVo();
//        pvVo.setAdsPvCountList(list);
//        pvVo.setPvCountResponses(pvCount);

//        response.setPvCount(list.stream().filter(item -> null != item.getPv()).mapToInt(AdsPvCount::getPv).sum());
//        response.setUvCount(list.stream().filter(item -> null != item.getUv()).mapToInt(AdsPvCount::getUv).sum());
//
//        CommonPageExtend pageInfo = CommonPageExtend.restPage(list);
//        pageInfo.setPvCount(response.getPvCount());
//        pageInfo.setUvCount(response.getUvCount());

//        return pvVo;
    }
}
